package com.workcell.workdata.xthings.kafka.sender;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.workcell.workdata.xthings.constant.DictEnum;
import com.workcell.workdata.xthings.constant.KafKaTopicConstant;
import com.workcell.workdata.xthings.dto.MqSendMessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author 廖博文
 * @Date 2023/5/25 10:00
 * @Description kafka消息队列消息发送
 */
@Slf4j
@Component
public class KafkaSendService {

	@Resource
	private KafkaTemplate<Object, Object> kafkaTemplate;
	@Resource
	private KafkaSendResultHandler kafkaSendResultHandler;

	/**
	 * 发送MQ队列
	 * @param sendParam
	 */
	public void sendMqMessage(MqSendMessageDTO sendParam) {
		try {
			log.info("Kafka消息队列发送日志，发送前参数：{}", JSON.toJSONString(sendParam));
			if (ObjectUtil.isNull(sendParam) || StrUtil.isBlank(sendParam.getTopic())
					|| StrUtil.isBlank(sendParam.getContent())) {
				log.info("Kafka消息队列发送日志，发送前失败：存在为空的参数");
				return;
			}
			String topic = sendParam.getTopic();
			String content = sendParam.getContent();
			kafkaTemplate.setProducerListener(kafkaSendResultHandler);
			kafkaTemplate.send(topic, content);
		} catch (Exception e) {
			log.error("Kafka消息队列发送日志，发送前异常：{}", e.getMessage());
		}
	}
}
